package io.github.yotta.dict.server.service.impl;

import io.github.yotta.common.api.exception.ServiceException;
import io.github.yotta.common.api.model.IdRequest;
import io.github.yotta.common.api.model.Paging;
import io.github.yotta.dict.api.enums.SysDictStatus;
import io.github.yotta.dict.api.param.SysDictAddParam;;
import io.github.yotta.dict.api.param.SysDictQueryParam;
import io.github.yotta.dict.api.param.SysDictUpdateParam;
import io.github.yotta.dict.api.vo.SysDictStatusVo;
import io.github.yotta.dict.api.vo.SysDictVo;
import io.github.yotta.dict.server.converter.SysDictConverter;
import io.github.yotta.dict.dao.entity.SysDict;
import io.github.yotta.dict.dao.entity.SysDictDao;
import io.github.yotta.dict.server.dto.SysDictStatusDto;
import io.github.yotta.dict.api.service.SysDictService;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author yotta-generator
* @date 2025-08-11 10:12:20
* User - 服务接口实现
*/
@Setter
public class SysDictServiceImpl implements SysDictService {


    private SysDictDao sysDictDao;

    private SysDictConverter sysDictConverter;

    private static final List<SysDictStatusDto> DEFAULT_STATUS = new ArrayList<>();

    static {
        for (SysDictStatus value : SysDictStatus.values()) {
            SysDictStatusDto sysDictStatusDto = new SysDictStatusDto();
            sysDictStatusDto.setCode(value.getCode());
            sysDictStatusDto.setMessage(value.getMessage());
            DEFAULT_STATUS.add(sysDictStatusDto);
        }
    }

    @Override
    public Boolean save(SysDictAddParam sysDictAddParam) {
        SysDict byCode = this.sysDictDao.getByCode(sysDictAddParam.getTenantId(), sysDictAddParam.getCode());
        if (byCode != null) {
            throw new ServiceException("code.is.exist");
        }
        SysDict sysDict = sysDictConverter.param2Entity(sysDictAddParam);
        sysDictDao.add(sysDict);
        return true;
    }

    public Boolean updateById(SysDictUpdateParam sysDictUpdateParam) {
		SysDict sysDict = sysDictConverter.param2Entity(sysDictUpdateParam);
		sysDictDao.updateById(sysDict);
        return true;
    }

    @Override
    public SysDictVo getById(IdRequest idParam) {
        SysDict byId = sysDictDao.getById(idParam.getTenantId(), idParam.getId());
        return sysDictConverter.entity2Vo(byId);
    }

    @Override
    public Boolean removeById(IdRequest id) {
        return sysDictDao.deleteById(id.getTenantId(), id.getId());
    }

    public List<SysDictVo> list(SysDictQueryParam queryParam) {
		List<SysDict> sysDictList= sysDictDao.list(queryParam);
		return sysDictList.stream().map(sysDictConverter::entity2Vo).collect(Collectors.toList());
    }

    @Override
    public Paging<SysDictVo> page(SysDictQueryParam queryParam) {
        Paging<SysDict> page = sysDictDao.page(queryParam);
        return page.map(sysDictConverter::entity2Vo);
    }


    @Override
    public List<SysDictStatusVo> listStatus() {
        return DEFAULT_STATUS.stream().map(sysDictConverter::convert).collect(Collectors.toList());
    }


    @Override
    public List<String> listCategory(String tenantId) {
        return sysDictDao.listCategory(tenantId);
    }



}
